package com.fintech.third.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import com.fintech.common.enums.RespStatus;
import com.fintech.common.util.ApiResponse;
import com.fintech.third.constant.ThirdMethodConstants;
import com.fintech.third.dao.MoxieTaskInfoMapper;
import com.fintech.third.model.MoxieTaskInfo;
import com.fintech.third.model.ThirdProperties;
import com.fintech.third.service.moxie.MoXieService;
import com.fintech.third.utils.SerialNumCreater;
import com.fintech.third.utils.ThirdPropertiesInitUtil;
import com.marvels.common.utils.MarvelsLogUtil;

/**
 * 魔蝎运营商任务
 * 
 * @author tangpw
 *
 */
public class MoXieCarrierTask implements Job {

	private static final ThirdProperties thirdProperties = ThirdPropertiesInitUtil.getThirdProperties();
	@Autowired
	private MoxieTaskInfoMapper moxieTaskInfoMapper;

//	@Autowired
//	private acLoanApplyInfoExtDragonService acLoanApplyInfoExtDragonService;
	
	@Autowired(required=true)
	private MoXieService freshMoXie;



	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		/*if (!Boolean.parseBoolean(thirdProperties.getIs_mx_new_programme_user())) {// 启用新方法
			return;
		}
		MarvelsLogUtil.getInstance().info("======魔蝎运营商定时任务开始");
		List<MoxieTaskInfo> list = moxieTaskInfoMapper.selectMoxieTaskInfo("1");
		if (list == null || list.size() < 1) {
			MarvelsLogUtil.getInstance().info("================没有需要执行的魔蝎运营商任务");
			return;
		}
		for (MoxieTaskInfo info : list) {
			try {
				int i = moxieTaskInfoMapper.updateMoxieTaskInfo(info.getId(), "1");// 先将状态改成认证中
				if (i < 1) {
					continue;
				}
				String loanId = info.getLoanId();
				String taskId = info.getTaskId();
				String mobile = info.getMobile();
				if (freshMoXie.taskIdStatusIsFail(taskId)) {//如果任务已经不能查得数据 不需要再查询 将状态改成-3
					MarvelsLogUtil.getInstance().info("==========魔蝎运营商任务已经失败不需要再次查询,moxie--needn't query任务id：" + info.getTaskId());
					moxieTaskInfoMapper.updateMoxieTaskInfo(info.getId(), "-3");
					continue;
				}
				MarvelsLogUtil.getInstance().info("=============>>>>运营商数据采集开始,moxie--need query,loanId:" + loanId + ",taskId:" + taskId + ",mobile" + mobile);
				Map<String, String> dataMap = new HashMap<String, String>();
				String serial_num = SerialNumCreater.creater("MX", loanId);
//
//				IntDataQueryLog intDataQueryLog1 = logMapper.selectExistRecord(ThirdMethodConstants.MX0001,
//						RespStatus.SUCCESS.getCode(), loanId);
//				IntDataQueryLog intDataQueryLog2 = logMapper.selectExistRecord(ThirdMethodConstants.MX0002,
//						RespStatus.SUCCESS.getCode(), loanId);
//				IntDataQueryLog intDataQueryLog3 = logMapper.selectExistRecord(ThirdMethodConstants.MX0003,
//						RespStatus.SUCCESS.getCode(), loanId);

//				if (intDataQueryLog1 != null) {
//					serial_num = intDataQueryLog1.getSerialNum();
//				} else if (intDataQueryLog2 != null) {
//					serial_num = intDataQueryLog2.getSerialNum();
//				} else if (intDataQueryLog3 != null) {
//					serial_num = intDataQueryLog3.getSerialNum();
//				}

				ApiResponse apiResponseMobileReport = new ApiResponse();
//				if (intDataQueryLog1 == null) {
					MarvelsLogUtil.getInstance().info("==================获取运营商用户报告请求开始,loanId:" + loanId + ",taskId:" + taskId);
					dataMap.put("task_id", taskId);
					apiResponseMobileReport = freshMoXie.obtainUserMobileReport(dataMap, mobile, serial_num, loanId,
							ThirdMethodConstants.channel);
					MarvelsLogUtil.getInstance().info("==================获取运营商用户报告结果:" + apiResponseMobileReport.getMsg() + ",loanId:"
							+ loanId + ",taskId:" + taskId);
//				} else {
//					apiResponseMobileReport.setCode(RespStatus.SUCCESS.getCode());
//				}

				ApiResponse apiResponseDataDetails = new ApiResponse();

//				if (intDataQueryLog2 == null) {
					MarvelsLogUtil.getInstance().info("==================获取运营商全部数据开始:loanId:" + loanId + ",taskId:" + taskId);
					apiResponseDataDetails = freshMoXie.obtainAllDataDetails(mobile, taskId, serial_num, loanId,
							ThirdMethodConstants.channel);
					MarvelsLogUtil.getInstance().info("==================获取运营商全部数据结果:" + apiResponseDataDetails.getMsg() + ",loanId:" + loanId
							+ ",taskId:" + taskId);

//				} else {
//					apiResponseDataDetails.setCode(RespStatus.SUCCESS.getCode());
//				}

				ApiResponse apiResponseReportV2 = new ApiResponse();

//				if (intDataQueryLog3 == null) {
					MarvelsLogUtil.getInstance().info("==================运营商查询用户报告v2开始,loanId:" + loanId + ",taskId:" + taskId);
					dataMap.put("task_id", taskId);
					apiResponseReportV2 = freshMoXie.obtainUserMobileReportV2(dataMap, mobile, serial_num, loanId,
							ThirdMethodConstants.channel);
					MarvelsLogUtil.getInstance().info("==================运营商查询用户报告v2结果:" + apiResponseReportV2.getMsg() + ",loanId:" + loanId
							+ ",taskId:" + taskId);
//				} else {
//					apiResponseReportV2.setCode(RespStatus.SUCCESS.getCode());
//				}

				String result = "";// -3认证失败（请求失败） -2落库失败 -1认证失败（返回数据为空 ） 2认证成功
									// 1认证中
				if (apiResponseReportV2.getCode() == RespStatus.SUCCESS.getCode()
						&& apiResponseDataDetails.getCode() == RespStatus.SUCCESS.getCode()
						&& apiResponseMobileReport.getCode() == RespStatus.SUCCESS.getCode()) {
					result = "2";
					
//					//更改运营商认证结果
//					try {
//						Map<String, Object> alipayAuth = new HashMap<String, Object>();
//						alipayAuth.put("operator_auth", "1");
//						boolean operateResult = acLoanApplyInfoExtDragonService.operateExtColumn(loanId, alipayAuth);
//						MarvelsLogUtil.getInstance().info("运营商认证结果写入，loanId：" + loanId + ",结果：" + operateResult);
//					} catch (Exception e) {
//						MarvelsLogUtil.getInstance().error("运营商认证结果写入异常，loanId：" + loanId, e);
//					}
				}

				if (apiResponseReportV2.getCode() == RespStatus.NODATA.getCode()
						|| apiResponseDataDetails.getCode() == RespStatus.NODATA.getCode()
						|| apiResponseMobileReport.getCode() == RespStatus.NODATA.getCode()) {
					result = "-1";
				}

				if (apiResponseReportV2.getCode() == RespStatus.DATABASE_ERROR.getCode()
						|| apiResponseDataDetails.getCode() == RespStatus.DATABASE_ERROR.getCode()
						|| apiResponseMobileReport.getCode() == RespStatus.DATABASE_ERROR.getCode()) {
					result = "-2";
				}

				if (apiResponseReportV2.getCode() == RespStatus.REQ_ERROR.getCode()
						|| apiResponseDataDetails.getCode() == RespStatus.REQ_ERROR.getCode()
						|| apiResponseMobileReport.getCode() == RespStatus.REQ_ERROR.getCode()) {
					result = "-4";
				}

				if ("2".equals(result)) {
					// 认证成功 插入历史记录表 删除原始表记录
					info.setResult(result);
					info.setUpdatedDate(new Date());
					moxieTaskInfoMapper.insertMoxieTaskInfoHistory(info);

					moxieTaskInfoMapper.deleteMoxieTaskInfo(info.getId());
				} else {
					// 其他 则修改原始表状态
					moxieTaskInfoMapper.updateMoxieTaskInfo(info.getId(), result);
				}
			} catch (Exception e) {
				MarvelsLogUtil.getInstance().error("==========魔蝎运营商任务[" + info.getId() + "]异常", e);
				moxieTaskInfoMapper.updateMoxieTaskInfo(info.getId(), "-1");
			}
		}

		MarvelsLogUtil.getInstance().info("==========魔蝎运营商任务完成");
		*/
	}
}
